package com.updesignsz.admin.controller;

import com.updesignsz.admin.service.UserService;
import com.updesignsz.admin.util.ResponseUtil;
import com.updesignsz.admin.vo.form.UserForm;
import com.updesignsz.commom.entity.User;
import com.updesignsz.commom.util.ResultVo;
import com.updesignsz.commom.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * TODO 后台用户接口
 * Created User: lx
 * Date: 2020/4/29 14:44
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/user")
    public void listAll(HttpServletResponse response) {
        ResultVo vo = new ResultVo();
        vo.setData(userService.listAll());
        ResponseUtil.outJson(vo.toJsonMsg(), response);

    }

    @GetMapping("/user/{userId}")
    public void getUser(@PathVariable Integer userId, HttpServletResponse response) {
        ResultVo vo = new ResultVo();
        vo.setData(userService.getById(userId));
        ResponseUtil.outJson(vo.toJsonMsg(), response);
    }


    @PostMapping("/user")
    public void addUser(@Validated(PostMapping.class) @RequestBody UserForm userForm, HttpServletResponse response) {
        ResultVo vo = new ResultVo();
        User user = convert(userForm);
        userService.saveUser(user);
        User entity = userService.getById(user.getId());
        entity.setId(user.getId());
        vo.setData(entity);
        ResponseUtil.outJson(vo.toJsonMsg(), response);
    }

    @PutMapping("/user")
    public void updateUser(@Validated(PutMapping.class) @RequestBody UserForm userForm, HttpServletResponse response) {
        ResultVo vo = new ResultVo();
        User user = convert(userForm);
        userService.updateUser(user);
        User entity = userService.getById(userForm.getId());
        entity.setId(userForm.getId());
        vo.setData(entity);
        ResponseUtil.outJson(vo.toJsonMsg(), response);
    }

    @PatchMapping("/user")
    public void updateRemark(@Validated(PatchMapping.class) @RequestBody UserForm userForm, HttpServletResponse response) {
        ResultVo vo = new ResultVo();
        User user = convert(userForm);
        userService.updateUser(user);
        vo.setData(userForm);
        ResponseUtil.outJson(vo.toJsonMsg(), response);
    }

    @DeleteMapping("/user/{userId}")
    public void deleteUser(@PathVariable Integer userId, HttpServletResponse response) {
        ResultVo vo = new ResultVo();
        userService.deleteUser(userId);
        ResponseUtil.outJson(vo.toJsonMsg(), response);
    }


    private User convert(UserForm userForm) {
        User user = new User();
        user.setId(userForm.getId());
        user.setUsername(userForm.getUsername());
        if (StringUtil.isNotEmpty(userForm.getPassword())) {
            user.setPassword(StringUtil.toMD5(userForm.getPassword()));
        }
        user.setRemark(userForm.getRemark());
        user.setPhoneNumber(userForm.getPhomeNumber());
        user.setRoleIdList(userForm.getRoles());
        return user;
    }
}
